package com.liuyu2783.redislock.web;

import com.alibaba.fastjson.JSONObject;
import com.liuyu2783.redislock.annotation.CacheLock;
import com.liuyu2783.redislock.annotation.CacheParam;
import com.liuyu2783.redislock.common.MyResponse;
import com.liuyu2783.redislock.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * com.liuyu2783.redislock.web
 *
 * @author liuyu07
 * @date 2019-6-27
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @CacheLock(prefix = "getUserById")
    @RequestMapping(value = "/getUserById", method = RequestMethod.POST)
    public MyResponse getUserById(@RequestBody JSONObject jsonObject){
        Random r = new Random();
        userService.getUser();
        MyResponse myResponse = new MyResponse().success(r.nextInt());
        logger.info("返回结果:{}", myResponse);
        return myResponse;
    }

    @CacheLock(prefix = "listUser")
    @RequestMapping(value = "/listUser", method = RequestMethod.POST)
    public MyResponse listUser(@CacheParam(name = "userId") @RequestParam("userId") String userId,
                               @CacheParam(name = "startDate") @RequestParam("startDate")String startDate){
        Random r = new Random();
        userService.getUser();
        MyResponse myResponse = new MyResponse().success(r.nextInt());
        logger.info("返回结果:{}", myResponse);
        return myResponse;
    }
}
